package com.ljf.eslab.lab.service.impl;

import com.ljf.eslab.lab.entity.TeacherEntity;
import com.ljf.eslab.lab.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.Query;

import com.ljf.eslab.lab.dao.CourseDao;
import com.ljf.eslab.lab.entity.CourseEntity;
import com.ljf.eslab.lab.service.CourseService;
import org.springframework.util.StringUtils;


@Service("courseService")
public class CourseServiceImpl extends ServiceImpl<CourseDao, CourseEntity> implements CourseService {

    @Autowired
    TeacherService teacherService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                new QueryWrapper<CourseEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByTid(Map<String, Object> params, Long tid) {
        QueryWrapper<CourseEntity> courseWrapper = new QueryWrapper<>();
        courseWrapper.eq("teacher_id",tid);
        IPage<CourseEntity> page = this.page(
                new Query<CourseEntity>().getPage(params),
                courseWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public HashSet<Integer> getByTID(Integer tid) {
        //找到老师id对应的所有课程
        List<CourseEntity> courses = this.baseMapper.selectList(
                new QueryWrapper<CourseEntity>().ge("teacher_id", tid));
        return (HashSet<Integer>) courses.stream()
                .map(CourseEntity::getId)
                .collect(Collectors.toSet());
    }

}